2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucer_Headers.h"
27 #include "jucer_PaintRoutineEditor.h"
28 #include "../model/jucer_ObjectTypes.h"
29 #include "jucer_JucerDocumentHolder.h"
32 //==============================================================================
33 PaintRoutineEditor::PaintRoutineEditor (PaintRoutine
& graphics_
,
34 JucerDocument
& document_
,
35 JucerDocumentHolder
* const docHolder
)
36 : graphics (graphics_
),
38 documentHolder (docHolder
),
40 componentOverlayOpacity (0.0f
)
44 setSize (document
.getInitialWidth(),
45 document
.getInitialHeight());
48 PaintRoutineEditor::~PaintRoutineEditor()
50 document
.removeChangeListener (this);
52 removeAllElementComps();
54 removeChildComponent (&lassoComp
);
58 void PaintRoutineEditor::removeAllElementComps()
60 for (int i
= getNumChildComponents(); --i
>= 0;)
62 PaintElement
* const e
= dynamic_cast <PaintElement
*> (getChildComponent (i
));
65 removeChildComponent (e
);
69 const Rectangle
<int> PaintRoutineEditor::getComponentArea() const
71 if (document
.isFixedSize())
73 return Rectangle
<int> ((getWidth() - document
.getInitialWidth()) / 2,
74 (getHeight() - document
.getInitialHeight()) / 2,
75 document
.getInitialWidth(),
76 document
.getInitialHeight());
80 return Rectangle
<int> (editorEdgeGap
, editorEdgeGap
,
81 getWidth() - editorEdgeGap
* 2,
82 getHeight() - editorEdgeGap
* 2);
86 //==============================================================================
87 void PaintRoutineEditor::paint (Graphics
& g
)
89 const Rectangle
<int> clip (getComponentArea());
91 g
.setOrigin (clip
.getX(), clip
.getY());
92 g
.reduceClipRegion (0, 0, clip
.getWidth(), clip
.getHeight());
94 graphics
.fillWithBackground (g
, true);
95 grid
.draw (g
, &graphics
);
98 void PaintRoutineEditor::paintOverChildren (Graphics
& g
)
100 if (componentOverlay
.isNull() && document
.getComponentOverlayOpacity() > 0.0f
)
101 updateComponentOverlay();
103 if (componentOverlay
.isValid())
105 const Rectangle
<int> clip (getComponentArea());
106 g
.drawImageAt (componentOverlay
, clip
.getX(), clip
.getY());
110 void PaintRoutineEditor::resized()
112 if (getWidth() > 0 && getHeight() > 0)
114 componentOverlay
= Image();
115 refreshAllElements();
119 void PaintRoutineEditor::updateChildBounds()
121 const Rectangle
<int> clip (getComponentArea());
123 for (int i
= 0; i
< getNumChildComponents(); ++i
)
125 PaintElement
* const e
= dynamic_cast <PaintElement
*> (getChildComponent (i
));
128 e
->updateBounds (clip
);
132 void PaintRoutineEditor::updateComponentOverlay()
134 if (componentOverlay
.isValid())
137 componentOverlay
= Image();
138 componentOverlayOpacity
= document
.getComponentOverlayOpacity();
140 if (componentOverlayOpacity
> 0.0f
)
142 if (documentHolder
!= 0)
143 componentOverlay
= documentHolder
->createComponentLayerSnapshot();
145 if (componentOverlay
.isValid())
147 componentOverlay
.multiplyAllAlphas (componentOverlayOpacity
);
153 void PaintRoutineEditor::visibilityChanged()
155 document
.getUndoManager().beginNewTransaction();
159 refreshAllElements();
160 document
.addChangeListener (this);
164 document
.removeChangeListener (this);
165 componentOverlay
= Image();
169 void PaintRoutineEditor::refreshAllElements()
172 for (i
= getNumChildComponents(); --i
>= 0;)
174 PaintElement
* const e
= dynamic_cast <PaintElement
*> (getChildComponent (i
));
176 if (e
!= 0 && ! graphics
.containsElement (e
))
177 removeChildComponent (e
);
182 for (i
= graphics
.getNumElements(); --i
>= 0;)
184 PaintElement
* const e
= graphics
.getElement (i
);
186 addAndMakeVisible (e
);
198 if (grid
.updateFromDesign (document
))
201 if (currentBackgroundColour
!= graphics
.getBackgroundColour())
203 currentBackgroundColour
= graphics
.getBackgroundColour();
208 if (componentOverlayOpacity
!= document
.getComponentOverlayOpacity())
210 componentOverlay
= Image();
211 componentOverlayOpacity
= document
.getComponentOverlayOpacity();
216 void PaintRoutineEditor::changeListenerCallback (ChangeBroadcaster
* source
)
218 refreshAllElements();
221 void PaintRoutineEditor::mouseDown (const MouseEvent
& e
)
223 if (e
.mods
.isPopupMenu())
227 m
.addCommandItem (commandManager
, CommandIDs::editCompLayout
);
228 m
.addCommandItem (commandManager
, CommandIDs::editCompGraphics
);
231 for (int i
= 0; i
< ObjectTypes::numElementTypes
; ++i
)
232 m
.addCommandItem (commandManager
, CommandIDs::newElementBase
+ i
);
238 addChildComponent (&lassoComp
);
239 lassoComp
.beginLasso (e
, this);
243 void PaintRoutineEditor::mouseDrag (const MouseEvent
& e
)
245 lassoComp
.toFront (false);
246 lassoComp
.dragLasso (e
);
249 void PaintRoutineEditor::mouseUp (const MouseEvent
& e
)
251 lassoComp
.endLasso();
253 if (e
.mouseWasClicked() && ! e
.mods
.isAnyModifierKeyDown())
255 graphics
.getSelectedElements().deselectAll();
256 graphics
.getSelectedPoints().deselectAll();
260 void PaintRoutineEditor::findLassoItemsInArea (Array
<PaintElement
*>& results
, const Rectangle
<int>& lasso
)
262 for (int i
= 0; i
< getNumChildComponents(); ++i
)
264 PaintElement
* const e
= dynamic_cast <PaintElement
*> (getChildComponent (i
));
266 if (e
!= 0 && e
->getBounds().expanded (-e
->borderThickness
, -e
->borderThickness
)
272 SelectedItemSet
<PaintElement
*>& PaintRoutineEditor::getLassoSelection()
274 return graphics
.getSelectedElements();
277 bool PaintRoutineEditor::isInterestedInFileDrag (const StringArray
& files
)
279 const File
f (files
[0]);
281 return f
.hasFileExtension ("jpg")
282 || f
.hasFileExtension ("jpeg")
283 || f
.hasFileExtension ("png")
284 || f
.hasFileExtension ("gif")
285 || f
.hasFileExtension ("svg");
288 void PaintRoutineEditor::filesDropped (const StringArray
& filenames
, int x
, int y
)
290 const File
f (filenames
[0]);
292 if (f
.existsAsFile())
294 Drawable
* d
= Drawable::createFromImageFile (f
);
300 document
.getUndoManager().beginNewTransaction();
302 graphics
.dropImageAt (f
,
303 jlimit (10, getWidth() - 10, x
),
304 jlimit (10, getHeight() - 10, y
));
306 document
.getUndoManager().beginNewTransaction();